<?php

namespace app\data\controller\report;

use think\admin\Controller;

/**
 * 数据统计-每日产出分析表
 * Class StatisticsDailyOutput
 * @package app\data\controller\report
 */
class StatisticsDailyOutput extends Controller
{
    /**
     * 绑定数据表
     * @var string
     */
    private $table = 'report_daily_output';

    /**
     * 每日产出分析表导出
     * @auth true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function indexExport()
    {
        $this->index();
    }

    /**
     * 数据列表处理
     * @param array $data
     */
    protected function _indexExport_page_filter(array &$data)
    {
        $this->_index_page_filter($data);
    }

    /**
     * 每日产出分析表
     * @auth true
     * @menu true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index()
    {
        // 设置页面标题
        $this->title = '每日产出分析表';

        $yesterday = date('Y-m-d', strtotime('-1 day'));
        $date = $this->request->request('date');
        if (empty($date)) {
            $date = $this->yesterday = $yesterday . ' - ' . $yesterday;
        } else {
            $this->yesterday = $yesterday;
        }

        [$minDate, $maxDate] = explode(' - ', $date);
        if ($minDate == $maxDate) {
            if ($minDate == date('Y-m-d')) {
                $this->error('今日数据尚未生成，请明天重试');
            }
            $this->showPeoplePowerInput = 1;
            $result = $this->_query($this->table)->where('date', $minDate)->order('id desc');
        } else {
            $this->showPeoplePowerInput = 0;

            if (empty($date)) {
                $query = $this->_query($this->table)->whereBetweenTime('date', $yesterday, $yesterday);
            } else {
                $query = $this->_query($this->table)->dateBetween('date');
            }
            $result = $query->group('from_city')->field('from_city, SUM(real_group_power) AS real_group_power, SUM(energy) AS energy, SUM(group_energy) AS group_energy, SUM(people_power) AS people_power, SUM(id) AS id');
        }

        // 数据查询分页
        if (input('output') == 'json') {
            $result = $result->page(true, false, false, 10000);
            $this->success('', $result);
        } else {
            $result->page(false, true, false, 10000);
        }

    }

    /**
     * 列表数据处理
     * @param array $data
     */
    protected function _index_page_filter(array &$data)
    {
        foreach($data as $sumItem) {
            foreach ($sumItem as $key => $value) {
                $str = 'sum_' . $key;
                if (!in_array($key, ['from_province', 'from_city', 'date', 'city_relation_id', 'cancel_order_json'])) {
                    if (!isset($this->sumList[$str])) {
                        $this->sumList[$str] = $value;
                    } else {
                        $this->sumList[$str] = $this->sumList[$str] + $value;
                    }
                }
            }
        }
    }

    /**
     * 编辑每日产出
     * @auth true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function edit()
    {
        $this->_form($this->table, 'form');
    }

    /**
     * 表单数据处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function _form_filter(array &$data)
    {
        if ($this->request->isGet()) {

        } else {
        }
    }

    /**
     * 表单结果处理
     * @param boolean $state
     */
    protected function _form_result(bool $state)
    {
        if ($state) {
            $this->success('保存成功');
        }
    }

    /**
     * 组均能效默认配置
     * @auth true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function config()
    {
        $this->title = '默认配置';
        $this->__sysconf('config');
    }

    /**
     * 显示并保存配置
     * @param string $template 模板文件名称
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    private function __sysconf(string $template)
    {
        if ($this->request->isGet()) {
            $this->fetch($template);
        }

        if ($this->request->isPost()) {
            $data = $this->request->post();
            foreach ($data as $k => $v) {
                sysconf($k, $v);
            }
            $this->success('配置保存成功！');
        }
    }
}